/*
extern unsigned char Digital-Input[MAX_BYTE_INPUT_IO];
extern unsigned char Digital-Output[MAX_BYTE_OUTPUT_IO];
*/
// SUL PORT A SONO  CONFIGURATI I SEGUENTI BIT DI CONTROLLO DELLO SHIFT REGISTER:
//      PA5: CLEAR                PORTA
//      PA6: ENABLE               PORTA 
//      PA7: SRCK (CLOCK BIT)     PORTA
//      PE0: LATCH (BYTE)         PORTE
//      PB1: DATA OUT             PORTB

#include <avr/io.h>
#include "sr_output.h"


#define SR_OUTPUT_PORT PORTE
#define SR_OUTPUT_RCLK PE7
#define SR_OUTPUT_CLEAR PE6

#define SR_OUTPUT_SPI_PORT PORTB
#define SR_OUTPUT_SCLK PB1
#define SR_OUTPUT_MOSI PB2



extern unsigned char Digital_Output[MAX_BYTE_OUTPUT_IO];


//*********************************************************************************
//                          SR_Output_Init
//*********************************************************************************
void SR_Output_Init (void){
  DO_Init();
  SR_Ouput_Clear ();
  SR_Output_Write ();
}
//*********************************************************************************
//                          SR_Output_Write
//*********************************************************************************
void SR_Output_Write (void){
  unsigned char volatile Byte_count,Bit_count,temp_out[MAX_BYTE_OUTPUT_IO],i;
  SR_Ouput_wait();
  //SR_Ouput_Enable ();
  SR_Ouput_wait();
  unsigned char BitToWrite;
  for (i=0;i<MAX_BYTE_OUTPUT_IO;i++) temp_out[(MAX_BYTE_OUTPUT_IO-1)-i]=Digital_Output[i];  //Inversione di BYTE 0=2 1=1 3=2
  for (i=0;i<MAX_BYTE_OUTPUT_IO;i++) temp_out[i]=ByteSwap(temp_out[i]);   // I byte sono stati invertiti e Swappati
      // applicare l'inversione dei singoli bit all'interno del byte
  for (Byte_count=0;Byte_count<MAX_BYTE_OUTPUT_IO;Byte_count++){
    for (Bit_count=0;Bit_count<8;Bit_count++){
      //PORTB_Bit1=(Digital_Output[Byte_count]>>Bit_count)&0x01;
	  BitToWrite=(temp_out[Byte_count]>>Bit_count)&0x01;
	  if (BitToWrite) SR_OUTPUT_SPI_PORT|=(1<<SR_OUTPUT_MOSI);
	  else SR_OUTPUT_SPI_PORT&=~(1<<SR_OUTPUT_MOSI);
	  
      SR_Ouput_wait();
      SR_OUTPUT_SPI_PORT|=(1<<SR_OUTPUT_SCLK); // bit clock = 1
      SR_Ouput_wait();
      SR_OUTPUT_SPI_PORT&=~(1<<SR_OUTPUT_SCLK); // bit clock = 0
      
    }
     
  }
    SR_Ouput_wait();
    SR_Ouput_ByteLatch ();
}



//*********************************************************************************
//                        SR_Ouput_ByteLatch
//*********************************************************************************
void SR_Ouput_ByteLatch (void){
  SR_OUTPUT_PORT |=(1<<SR_OUTPUT_RCLK);  //Latch =1
  SR_Ouput_wait();
  SR_OUTPUT_PORT &=~(1<<SR_OUTPUT_RCLK); //Latch =0
}

//*********************************************************************************
//                        SR_Ouput_Clear
//*********************************************************************************
void SR_Ouput_Clear (void){
  SR_OUTPUT_PORT&=~(1<<SR_OUTPUT_CLEAR); //Clear = 0
  SR_Ouput_wait();
  SR_OUTPUT_PORT|=(1<<SR_OUTPUT_CLEAR);  //Clear = 1
}

//*********************************************************************************
//                        SR_Ouput_wait
//*********************************************************************************
void SR_Ouput_wait (void){
  unsigned int i;
  for (i=0;i<100;i++);
  i++;
}
